package com.xiaomaoguai.user.action;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.Comparator;

import javax.imageio.ImageIO;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

/**
 * @Description :读取PDF
 * @FileName: Test.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月12日 下午7:01:39
 * @Version:V1.00
 */
public class Test {

	public static void main(String[] args) {

		Test test = new Test();

		PDFFile pdfFile = test.getPdfFile("D:/跟我学Shiro教程.pdf");
		test.pdf2Images(pdfFile, "D://test22/", "");
	}

	private PDFFile getPdfFile(String filePath) {
		try {
			File file = new File(filePath);
			RandomAccessFile raf = new RandomAccessFile(file, "r");
			FileChannel channel = raf.getChannel();
			ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
			PDFFile pdfFile = new PDFFile(buf);
			return pdfFile;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	private boolean pdf2Images(PDFFile pdfFile, String imageSavePath, String fileName) {
		if (pdfFile == null) {
			return false;
		}
		// 将转换后图片存放于path路径下
		String path = imageSavePath + fileName + "\\";
		File filePath = new File(path);
		if (!filePath.exists()) { // 判断以文件名命名的文件夹是否存在.
			filePath.mkdirs();
		}
		// 取得当前文件夹下的所有jpg格式的文件名.
		String[] imageNames = filePath.list(new ImageFilter());
		if (imageNames.length == 0) { // 当前文件夹下没有文件.
			// 将pdf文档按页转为图片.
			String imagePath = "";
			try {
				// 对转换页数进行限制,最多只转换前maxPage页.
				int pages = pdfFile.getNumPages();
				if (pages > 5) {
					pages = 5;
				}
				for (int i = 1; i <= pages; i++) {
					// draw the page to an image
					PDFPage page = pdfFile.getPage(i);
					// get the width and height for the doc at the default zoom
					Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox()
							.getHeight());
					// generate the image
					Image img = page.getImage(rect.width, rect.height, rect, null, true, true);

					BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);

					tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null);

					imagePath = path + i + ".jpg";

					ImageIO.write(tag, "JPEG", new File(imagePath));

				}
			} catch (Exception ex) {
				ex.printStackTrace();
				return false;
			}
		}
		// 取得当前文件夹下的所有jpg格式的文件名.
		imageNames = filePath.list(new ImageFilter());
		// 对文件名排序.
		Arrays.sort(imageNames, new FileNameComparator());
		return true;
	}

	// 图片后缀名过滤类

	// 图片jpg过滤器类
	class ImageFilter implements FilenameFilter {

		public boolean isImageFile(String fileName) {
			if (fileName.toLowerCase().endsWith("jpg")) {
				return true;
			} else {
				return false;
			}
		}

		public ImageFilter() {
		}

		public boolean accept(File dir, String name) {
			return isImageFile(name);
		}
	}

	// 文件名称比较类

	class FileNameComparator implements Comparator {

		public final int compare(Object first, Object second) {
			String[] fir = ((String) first).split("\\.");
			String[] sec = ((String) second).split("\\.");

			int firstPage = Integer.parseInt(fir[0]);
			int secondPage = Integer.parseInt(sec[0]);
			int diff = firstPage - secondPage;
			if (diff > 0)
				return 1;
			if (diff < 0)
				return -1;
			else
				return 0;
		}
	}

}
